package com.wmyg.antifake.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


/*���Ͷ���*/
public class SmsUtils {

	public static final String url = "http://39.106.132.113/wdms/alisms/sendsms";

	public static boolean sendSms(String mobile, String content, String calleridentity) {
		try {
			String result = sendAliSms(mobile,content);
			System.out.println("******"+result);
			JSONObject jsonObject=JSONObject.parseObject(result);
			if(("OK".equals(jsonObject.getString("Code")))) {
				return true;
			} else {
				throw new IOException("sms send result:" + result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public static String getRandomStr(int length, int type) {
		String str = "";
		int beginChar = 'a';
		int endChar = 'z';
		if (type == 1) {// 只有数字
			beginChar = 'z' + 1;
			endChar = 'z' + 10;
		} else if (type == 2) {// 只有小写字母
			beginChar = 'a';
			endChar = 'z';
		} else { // 有数字和字母
			beginChar = 'a';
			endChar = 'z' + 10;
		}
		// 生成随机类
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			int tmp = (beginChar + random.nextInt(endChar - beginChar));
			// 大于'z'的是数字
			if (tmp > 'z') {
				tmp = '0' + (tmp - 'z');
			}
			str += (char) tmp;
		}

		return str.toUpperCase();
	}
	public static String getSmsYzm(){
		return getRandomStr(6, 1);
	}

	public static String sendAliSms(String phone,String code){
		DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIeXv2ihBdYmn9", "Wr86YZZup2IjUdvDPNE6LL6NsfQM1J");
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		//request.setProtocol(ProtocolType.HTTPS);
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		Map codeMap=new HashMap();
		codeMap.put("code",code);
		String codeJson=JSONObject.toJSONString(codeMap);
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", "妙码");
		request.putQueryParameter("TemplateCode", "SMS_156275479");
		request.putQueryParameter("TemplateParam", codeJson);

		try {
			CommonResponse response = client.getCommonResponse(request);
			return response.getData();
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static boolean sendAuditSms(String mobile) {
		try {
			String result = sendAliAuditSms(mobile);
			System.out.println("******"+result);
			JSONObject jsonObject=JSONObject.parseObject(result);
			if(("OK".equals(jsonObject.getString("Code")))) {
				return true;
			} else {
				throw new IOException("sms send result:" + result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static String sendAliAuditSms(String phone){
		DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIeXv2ihBdYmn9", "Wr86YZZup2IjUdvDPNE6LL6NsfQM1J");
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		//request.setProtocol(ProtocolType.HTTPS);
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		Map codeMap=new HashMap();
		//codeMap.put("code",code);
		//	String codeJson=JSONObject.toJSONString(codeMap);
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", "妙码");
		request.putQueryParameter("TemplateCode", "SMS_168340117");
		//	request.putQueryParameter("TemplateParam", codeJson);

		try {
			CommonResponse response = client.getCommonResponse(request);
			return response.getData();
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static boolean sendNotAuditSms(String mobile) {
		try {
			String result = sendAliNotAuditSms(mobile);
			System.out.println("******"+result);
			JSONObject jsonObject=JSONObject.parseObject(result);
			if(("OK".equals(jsonObject.getString("Code")))) {
				return true;
			} else {
				throw new IOException("sms send result:" + result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public static String sendAliNotAuditSms(String phone){
		DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIeXv2ihBdYmn9", "Wr86YZZup2IjUdvDPNE6LL6NsfQM1J");
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		//request.setProtocol(ProtocolType.HTTPS);
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		Map codeMap=new HashMap();
		//codeMap.put("code",code);
		//	String codeJson=JSONObject.toJSONString(codeMap);
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", "妙码");
		request.putQueryParameter("TemplateCode", "SMS_168340128");
		//	request.putQueryParameter("TemplateParam", codeJson);

		try {
			CommonResponse response = client.getCommonResponse(request);
			return response.getData();
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return "";
	}
	public static void main(String[] args) {
		boolean s=sendSms("17600607700","123456","");
		System.out.println(String.format(":%s,", s));
	}
}
